package core.toolbox.ID;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import core.shiro.ShiroUtils;
import core.toolbox.Period.PeriodUtils;
import org.apache.shiro.subject.Subject;

import java.text.DecimalFormat;

/**
 * Created by Administrator on 2017/3/15.
 */
public class IdUtils {

    public static int getJzpzId(){
        StringBuilder sb = new StringBuilder();
        int userId = ShiroUtils.getUserId();
        int currentPeriod = PeriodUtils.getCurrentPeriod();
        String jzpzid = new DecimalFormat("000000").format(getAutoIncrement("jzpzid"));
        sb.append(userId).append(currentPeriod).append(jzpzid);
        return Integer.parseInt(sb.toString());
    }

    public static int getAutoIncrement(String sequenceName){
        int id = Db.queryInt("select nextval(?)",sequenceName);
        if(id == 0)
            throw new RuntimeException("序列名不存在["+sequenceName+"]");
        return id;
    }

    public static void createSequence(String sequenceName){
        Db.save("sequence", new Record().set("seq_name",sequenceName).set("current_val",1).set("increment_val",1));
    }



}
